<?php
namespace app\api\controller\yluser;
use x\my\Curl;
use think\image;
class Sharepic extends Base{

		//用户分享9图
    public function share(){
        $d = $this->post;
        $rule = [
            'goods_id'=>'require',
        ];
        $msg = [
            'goods_id.require' => '请输入产品id',
        ];
        check($rule,$msg,$d);
		$goods_id = $d['goods_id'];
				//$this->checkLogin();

        $rs = $this->db->find('goods',$goods_id,'content,goods_price_id');
        $v = strtolower(htmlspecialchars_decode($rs['content']));
        $preg = '/<img.*?src=[\"|\']?\/uploads(.*?)[\"|\'].*?[\/]?>/i';
        preg_match_all($preg, $v, $imgArr);
        $temp = [];
        if(isset($imgArr[1])){
            foreach($imgArr[1] as $k=>$img){
                if ($k<=5){
                  $temp[] = "/uploads".$img;  
                }else if(count($imgArr[1])-3<$k){
                  $temp[] = "/uploads".$img;  
                }
            }
        }

        $dir = APP_PATH.'../public/uploads/sharepic/'.$goods_id.'/';
        com_mkdir($dir);
        //图片
        $pics = [];
        foreach($temp as $k => $v){
			$path = APP_PATH.'../public'.$v;
            $pic = $this->_thumb($path,$goods_id,$k);
            if ($pic) $pics[] = $pic;
        }
        $user_id = $this->userinfo?$this->userinfo['id']:0;
		$pics[] = $this->_userQrcode($goods_id, $user_id);
        suc($pics);
    }

		//生成图片
    private function _thumb($path,$goods_id,$k){
        if (!is_file($path)) return "";
        $image = \think\Image::open($path);
				
        if(in_array($image->type(),['jpg','jpeg','png','gif'])){
            $dir = APP_PATH.'../public/uploads/sharepic/'.$goods_id.'/';
            $filename = $dir.$k.".".$image->type();
						
			//存在就直接返回
			if(file_exists($filename)) return "/uploads/sharepic/$goods_id/$k.".$image->type();
            if (!is_dir(dirname($filename))) {
                if(!com_mkdir(dirname($filename))){
                    err('Without directory permissions, upload images fail');
                }
            }
            $image->thumb(400, 400,\think\Image::THUMB_SCALING)->save($filename);
            $arr = explode('uploads',$filename);
            $url = "/uploads/".$arr[1];
            return $url;
        }else{
					err('图片类型错误');
				}
    }
		
		//生成用户二维码
		private function _userQrcode($goods_id, $user_id){
			$filename = APP_PATH."../public/uploads/sharepic/$goods_id/user_$user_id.png";
			if(file_exists($filename)) return "/uploads/sharepic/$goods_id/user_$user_id.png";
			
			$access_token = m('ThreeTemp','c')->getWxAccessToken();
			if (!$access_token) err('获取微信信息失败');
			#微信小程序码
			$data = [
					'path' => "/pages/shop/goodsdetail/index?id={$goods_id}&invitecode=".en_id($user_id),
					'width' => '400',
			];
			$xcx_qrcode = Curl::post("https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token,json_encode($data));

			file_put_contents($filename,$xcx_qrcode);
			return "/uploads/sharepic/$goods_id/user_$user_id.png";
		}
}
?>
